package servlet.life.cycle;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Servlet implementation class TestServlet1
 */
public class TestServlet1 extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public TestServlet1() {
        System.out.println("TestServlet1 构造方法被执行....");
    }

	@Override
	public void init() throws ServletException {
		System.out.println("TestServlet1 init()方法被执行....");
	}



	@Override
	public void destroy() {
		System.out.println("TestServlet1 destroy方法被执行....");
	}



	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
		System.out.println("TestServlet1的doGet()方法被执行...");
		response.setContentType("text/html;charset=utf-8");
		PrintWriter out = response.getWriter();
		out.println("<!DOCTYPE html PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\" \"http://www.w3.org/TR/html4/loose.dtd\">");
		out.println("<html>");
		out.println("<head>");
		out.println("<meta http-equiv=\"Content-Type\" content=\"text/html; charset=UTF-8\">");
		out.println("<title>Insert title here</title>");
		out.println("</head>");
		out.println("<body>");
		out.println("	<h1>您好，我是 TestServlet1</h1>");
		out.println("	<hr/>");
		out.println("	<a href=\"servlet/HelloServlet\">Get方式请求HelloServlet</a>");
		out.println("	<form action=\"servlet/HelloServlet\" method=\"post\">");
		out.println("		<input type=\"submit\" value=\"Post方式请求HelloServlet\" />");
		out.println("	</form>");
		out.println("</body>");
		out.println("</html>");
		
		
		response.getWriter().append("Served at: ").append(request.getContextPath());
	
	
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		System.out.println("TestServlet1的doPost()方法被执行...");
		
		doGet(request, response);
	}

}